//
//  XMStatusFrame.m
//  XMWeibo
//
//  Created by chi on 14-8-25.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMStatusFrame.h"

#import "XMStatus.h"
#import "XMPhotosView.h"

/** 微博头像宽高 */
#define kIconHW (50.0)

/** 会员图片宽高 */
#define kVipHW (15.0)

/** 微博配图宽高 */
//#define kPhotoHW (80.0)

/** 转发微博配图宽高 */
#define kRetweetPhotoHW kPhotoHW



@implementation XMStatusFrame

- (void)setStatus:(XMStatus *)status
{
    _status = status;
    
    //topView
    CGFloat topViewW = [UIScreen mainScreen].applicationFrame.size.width - kStatusCellEdge * 2;
    CGFloat topViewH = 0.0;
    CGFloat topViewX = kStatusCellEdge;
    CGFloat topViewY = 0.0;
    
    //微博头像
    CGFloat iconViewW = kIconHW;
    CGFloat iconViewH = kIconHW;
    CGFloat iconViewX = kStatusSubViewsMargin;
    CGFloat iconViewY = kStatusSubViewsMargin * 2;
    self.iconViewF = CGRectMake(iconViewX, iconViewY, iconViewW, iconViewH);
    
    //微博昵称
    CGFloat nameLabelX = CGRectGetMaxX(_iconViewF) + kStatusSubViewsMargin;
    CGFloat nameLabelY = kStatusSubViewsMargin * 2;
    self.nameLabelF = (CGRect){CGPointMake(nameLabelX, nameLabelY), [_status.user.name sizeWithFont:kStatusNameLabelFont]};
    
    //会员图片
    CGFloat vipViewW = kVipHW;
    CGFloat vipViewH = kVipHW;
    CGFloat vipViewX = CGRectGetMaxX(self.nameLabelF) + kStatusSubViewsMargin;
    CGFloat vipViewY = kStatusSubViewsMargin * 2;
    self.vipViewF = CGRectMake(vipViewX, vipViewY, vipViewW, vipViewH);
    
    //微博时间
    CGFloat timeLabelX = CGRectGetMaxX(_iconViewF) + kStatusSubViewsMargin;
    CGFloat timeLabelY = CGRectGetMaxY(self.nameLabelF) + kStatusSubViewsMargin;
    self.timeLabelF = (CGRect){CGPointMake(timeLabelX, timeLabelY), [_status.statusTime sizeWithFont:kStatusTimeLabelFont]};
    
    //微博来源
    CGFloat sourceLabelX = CGRectGetMaxX(_timeLabelF) + kStatusSubViewsMargin;
    CGFloat sourceLabelY = CGRectGetMaxY(self.nameLabelF) + kStatusSubViewsMargin;
    self.sourceLabelF = (CGRect){CGPointMake(sourceLabelX, sourceLabelY), [_status.source sizeWithFont:kStatusSourceLabelFont]};
    
    //微博内容
    CGFloat contentLabelX = kStatusSubViewsMargin;
    CGFloat contentLabelY = MAX(CGRectGetMaxY(self.timeLabelF), CGRectGetMaxY(self.iconViewF)) + kStatusSubViewsMargin;
    self.contentLabelF = (CGRect){CGPointMake(contentLabelX, contentLabelY), [_status.text sizeWithFont:kStatusContentLabelFont constrainedToSize:CGSizeMake(topViewW - kStatusSubViewsMargin, MAXFLOAT)]};
    
    
    ////////////////////////////////////////////////////////////
    
    
    
    //被转发微博
    
    XMStatus *retweetStatus = _status.retweeted_status;
    
    if (retweetStatus) {
        
        CGFloat retweetViewX = contentLabelX;
        CGFloat retweetViewY = CGRectGetMaxY(self.contentLabelF) + kStatusSubViewsMargin;
        CGFloat retweetViewH = 0.0;
        CGFloat retweetViewW = topViewW - kStatusSubViewsMargin * 2;
        
        //被转发微博昵称
        CGFloat retweetNameLabelX = kStatusSubViewsMargin;
        CGFloat retweetNameLabelY = kStatusSubViewsMargin * 2;
        self.retweetNameLabelF = (CGRect){CGPointMake(retweetNameLabelX, retweetNameLabelY), [retweetStatus.user.name sizeWithFont:kRetweetStatusNameLabelFont]};
        
        //被转发微博内容
        CGFloat retweetContentLabelX = retweetNameLabelX;
        CGFloat retweetContentLabelY = CGRectGetMaxY(self.retweetNameLabelF) + kStatusSubViewsMargin;
        self.retweetContentLabelF = (CGRect){CGPointMake(retweetContentLabelX, retweetContentLabelY), [retweetStatus.text sizeWithFont:kRetweetStatusContentLabelFont constrainedToSize:CGSizeMake(topViewW - kStatusSubViewsMargin * 2, MAXFLOAT)]};
        
        //被转发微博配图
        if (retweetStatus.pic_urls.count > 0) {//转发的微博并且有配图
//            CGFloat retweetPhotoViewH = kThumbnailPhotoHW * 3;
//            CGFloat retweetPhotoViewW = kThumbnailPhotoHW * 3;
//            CGFloat retweetPhotoViewX = retweetContentLabelX;
//            CGFloat retweetPhotoViewY = CGRectGetMaxY(self.retweetContentLabelF) + kStatusSubViewsMargin;
            
            self.retweetPhotoViewF = [self getPhotoFrameWithPhotos:retweetStatus.pic_urls andXPos:retweetContentLabelX andLabelFrame:self.retweetContentLabelF];
            //CGRectMake(retweetPhotoViewX, retweetPhotoViewY, retweetPhotoViewW, retweetPhotoViewH);
            
            retweetViewH = CGRectGetMaxY(self.retweetPhotoViewF) + kStatusSubViewsMargin;
        }
        else {//转发的微博但是没有配图
            retweetViewH = CGRectGetMaxY(self.retweetContentLabelF) + kStatusSubViewsMargin;
        }
        
        
        self.retweetViewF = CGRectMake(retweetViewX, retweetViewY, retweetViewW, retweetViewH);
        
        topViewH = CGRectGetMaxY(self.contentLabelF) + retweetViewH + kStatusSubViewsMargin;
    }
    else if (status.pic_urls.count > 0){//原创微博、并且有配图
       
        
        self.photoViewF = [self getPhotoFrameWithPhotos:status.pic_urls andXPos:topViewX andLabelFrame:self.contentLabelF];
        //CGRectMake(photoViewX, photoViewY, photoViewW, photoViewH);
        
        topViewH = CGRectGetMaxY(self.photoViewF) + kStatusSubViewsMargin;

    }
    else {//原创微博、没配图
        topViewH = CGRectGetMaxY(self.contentLabelF) + kStatusSubViewsMargin;
    }
    
    self.topViewF = CGRectMake(topViewX, topViewY, topViewW, topViewH);
    
    CGFloat toolsBarViewH = kToolsBarH;
    CGFloat toolsBarViewW = topViewW;
    CGFloat toolsBarViewX = topViewX;
    CGFloat toolsBarViewY = CGRectGetMaxY(self.topViewF);
    
    self.toolsBarViewF = CGRectMake(toolsBarViewX, toolsBarViewY, toolsBarViewW, toolsBarViewH);
    
    self.cellHeight = CGRectGetMaxY(self.toolsBarViewF) + kStatusCellEdge * 2;
        
}

#pragma mark - 计算配图frame

- (CGRect)getPhotoFrameWithPhotos:(NSArray*)picArray andXPos:(CGFloat)viewX andLabelFrame:(CGRect)labelFrame
{
    CGFloat photoViewH = 0.0;//kThumbnailPhotoHW * 3;
    CGFloat photoViewW = 0.0;//kThumbnailPhotoHW * 3;
    
    NSUInteger picCount = picArray.count;
    if (picCount < 4) {
        photoViewH = kThumbnailPhotoHW;
        photoViewW = kThumbnailPhotoHW * picCount;
    }
    else if (picCount < 7)
    {
        photoViewH = kThumbnailPhotoHW * 2;
        photoViewW = kThumbnailPhotoHW * 3;
    }
    else
    {
        photoViewH = kThumbnailPhotoHW * 3;
        photoViewW = kThumbnailPhotoHW * 3;
    }
    
    
    CGFloat photoViewX = viewX + labelFrame.origin.x;
    CGFloat photoViewY = CGRectGetMaxY(labelFrame) + kStatusSubViewsMargin;
    
    return CGRectMake(photoViewX, photoViewY, photoViewW, photoViewH);
}

@end
